//
//  PayOrderSetupOne.m
//  HB
//
//  Created by 陶秋安 on 16/4/7.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "PayOrderSetupOne.h"
#import "PayOrderSetupTwo.h"
#import "SubmitOrderOperation.h"
#import "PayRuleCell.h"

@implementation PayOrderSetupOne

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    arrayRules=[[NSArray alloc]init];
    
    _tvPayRules.dataSource=self;
    _tvPayRules.delegate=self;
    
    self.tvPayRules.autoresizingMask= UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    
    [self loadPayRule];
    
}
- (IBAction)btnBackClick:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)myTask
{
    
    operation=[[SubmitOrderOperation alloc] initWithDelegate:self];
    //开始保存数据
    
    NSString *djID=[Common getUserDefaults:@"djID"];
    
    NSString *url= [FxGlobal getServiceUrl:@"Pay" andFuncName:@"submitOrder"];
    
    NSString *md5Str= [NSString stringWithFormat: @"djID=%@&ruleID=%ld&key=tqa",djID,(long)ruleID];
    
    NSString *sign = [Common createMD5:md5Str];
    
    NSString *body=[NSString stringWithFormat:@"{\"djID\":\"%@\",\"ruleID\":\"%ld\",\"sign\":\"%@\",\"clicentid\":\"1\"}",djID,(long)ruleID,sign];
    
    NSDictionary *dictInfo = @{@"url":url,@"body":body};
    
    [operation setOpInfo:dictInfo];
    
    [operation post];
    
}

-(void)loadPayRule
{
    operation=[[SubmitOrderOperation alloc] initWithDelegate:self];
    //开始保存数据
    
    NSString *url= [FxGlobal getServiceUrl:@"Pay" andFuncName:@"PayRules"];
    
    NSDictionary *dictInfo = @{@"url":url};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:0];
    
}

-(void)opSuccessForPost:(id)data
{
    //self->isLoading=NO;

    
    operation=nil;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data1 = [dataStr objectFromJSONString];
    
    NSString *v=[data1 objectForKey:@"result"];
    
    if([v isEqualToString:@"1"])
    {
        NSString *orderCode=  [data1 objectForKey:@"orderCode"];
        
        NSString *amount=  [data1 objectForKey:@"amount"];
        
        NSString *point=  [data1 objectForKey:@"point"];
        
        [Common setUserDefaults:@"OrderCode" andValue:orderCode];
        
        [self openNextNib:orderCode andAmount:amount andPoint:point];
    }
}

-(void)opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    operation=nil;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *dataParse  = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    if ([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
    {
        arrayRules = [dataParse objectForKey:@"data"];
    }
    
    [self.tvPayRules performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

-(void)openNextNib:(NSString *)orderCode andAmount:(NSString *)amount andPoint:(NSString *)point
{
    PayOrderSetupTwo *post=[[PayOrderSetupTwo alloc]init];
    
    post.orderCode=orderCode;
    
    post.amount = amount;
    
    post.point= point;
    
    [self  presentViewController:post animated:true completion:nil];
    
}


#pragma mark 设置表格有几个数据区域
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

#pragma mark 设置表格的行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 44;
}

#pragma mark 设置表格有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  arrayRules.count;
}

#pragma mark 表格绑定数据
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *idefine = @"PayRuleCell";
    
    PayRuleCell *cell = [tableView dequeueReusableCellWithIdentifier:idefine];
    
    if (cell==nil)
    {

        NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:idefine owner:tableView options:nil];
        cell = [Objects objectAtIndex:0];

    }
    
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    
    NSInteger i=  indexPath.row;
    
    NSDictionary *rule= [arrayRules objectAtIndex:i];
    
    cell.ruleID = [[rule objectForKey:@"id"] integerValue];
    
    NSString *money=[rule objectForKey:@"Money"];
    
    cell.lblMoney.text =   [NSString stringWithFormat:@"￥%@",money];
    
    NSString *point=[rule objectForKey:@"Point"];
    
    cell.lblPoint.text = [NSString stringWithFormat:@"%@积分",point];
    
    NSInteger gift= [[rule objectForKey:@"Gift"] integerValue];
    
    if(gift>0)
    {
        cell.lblGiftPoint.hidden= NO;
        
        cell.lblGiftPoint.text = [NSString stringWithFormat:@"赠送%@积分",[rule objectForKey:@"Gift"]];
    }
    else
    {
        cell.lblGiftPoint.hidden=YES;
    }
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    PayRuleCell *cell = (PayRuleCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    ruleID =  cell.ruleID;
    
    self->isLoading=YES;
    
    [self waitingFor:@"正在创建订单" andMethod:@selector(myTask) onTarget:self];
}

@end
